package io.github.talelin.latticy.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.spu.SpuDTO;
import io.github.talelin.latticy.model.ArticleDO;
import io.github.talelin.latticy.model.SpuDO;
import io.github.talelin.latticy.model.WxUserDO;
import io.github.talelin.latticy.service.SpuService;
import io.github.talelin.latticy.service.WxUserService;
import io.github.talelin.latticy.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;

/**
 * @author generator@lms
 * @since 2021-04-27
 */
@RestController
@RequestMapping("/api/spu")
@Validated
public class SpuApiController {
    @Autowired
    private SpuService spuService;

    @Autowired
    private WxUserService wxUserService;

    @PostMapping("")
    public CreatedVO create(@RequestBody @Validated SpuDTO dto) {
        System.out.println(dto);
        WxUserDO wxUserDO = wxUserService.getById(dto.getUid());
        if (wxUserDO == null) {
            throw new NotFoundException(20000);
        }
        SpuDO spu = new SpuDO();
        BeanUtils.copyProperties(dto, spu);
        spu.setUsername(wxUserDO.getUsername());
        spu.setUserAvatar(wxUserDO.getAvatar());
        System.out.println(spu);
        int insert = spuService.getBaseMapper().insert(spu);
        if (insert == 1) {
            return new CreatedVO("商品发布成功！");
        } else {
            throw new NotFoundException(80401);
        }
    }

    @PutMapping("/{id}")
    public UpdatedVO update(
            @RequestBody SpuDTO dto,
            @PathVariable @Positive(message = "{id.positive}") Integer id) {
        SpuDO spu = spuService.getBaseMapper().selectOne(new QueryWrapper<SpuDO>().eq("id", id).eq("uid", dto.getUid()));
        if (spu == null) {
            throw new NotFoundException(80400);
        }
        BeanUtils.copyProperties(dto, spu);
        int update = spuService.getBaseMapper().updateById(spu);
        if (update == 1) {
            return new UpdatedVO("商品修改成功！");
        } else {
            throw new NotFoundException(80402);
        }
    }

    @DeleteMapping("/{id}")
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id,
                            @RequestParam Integer uid) {
        int delete = spuService.getBaseMapper().delete(new QueryWrapper<SpuDO>().eq("id", id).eq("uid", uid));
        if (delete == 1) {
            return new DeletedVO("商品删除成功！");
        } else {
            throw new NotFoundException(80400);
        }
    }

    @GetMapping("/{id}")
    public SpuVO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        return spuService.getSpu(id);
       // SpuDO spu = spuService.getById(id);
       // if (spu == null) {
       //     throw new NotFoundException(80400);
       // }
       ///* WxUserDO wxUser = wxUserService.getById(spu.getUid());
       // SpuVO spuVO = new SpuVO();
       // BeanUtils.copyProperties(spu, spuVO);
       // spuVO.setUsername(wxUser.getUsername());
       // spuVO.setUserAvatar(wxUser.getAvatar());
       // System.out.println(spuVO);*/
       // return spu;
    }

    @GetMapping("/page")
    public PageResponseVO<SpuDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            String tags
    ) {
        System.out.println(tags);
        Page<SpuDO> pager = new Page<>(page, count);
        IPage<SpuDO> paging;
        if (tags == null || tags.isEmpty()) {
            paging = spuService.getBaseMapper().selectPage(pager, new QueryWrapper<SpuDO>()
                    .orderByDesc("create_time"));
        } else {
            paging = spuService.getBaseMapper().selectPage(pager, new QueryWrapper<SpuDO>()
                    .eq("tags", tags)
                    .orderByDesc("create_time"));
        }
        return PageUtil.build(paging);
    }

    @GetMapping("/pageUser")
    public PageResponseVO<SpuDO> pageUser(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam Integer uid
    ) {
        Page<SpuDO> pager = new Page<>(page, count);
        IPage<SpuDO> paging = spuService.getBaseMapper().selectPage(pager, new QueryWrapper<SpuDO>().eq("uid", uid));
        return PageUtil.build(paging);
    }

    //搜索商品
    @GetMapping("/search")
    public PageResponseVO<SpuDO> searchPage(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            String tags,
            @RequestParam String key
    ) {
        System.out.println(tags);
        Page<SpuDO> pager = new Page<>(page, count);
        IPage<SpuDO> paging;
        if (tags == null || tags.isEmpty()) {
            paging = spuService.getBaseMapper().selectPage(pager, new QueryWrapper<SpuDO>()
                    .like("title",key));
        } else {
            paging = spuService.getBaseMapper().selectPage(pager, new QueryWrapper<SpuDO>()
                    .eq("tags", tags)
                    .like("title",key));
        }
        return PageUtil.build(paging);
    }

}
